﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using Microsoft.Practices.Unity; 
using System.Reflection;
using System.Xml.Linq; 
using System.Configuration;
using System.Web;
using Exhibition.Service;
using Exhibition.Common.Web;
using Exhibition.Common.Helper;

namespace Exhibition.Common
{
    public class ManagerWebInit
    {
        protected static BindingFlags BINDING_FLAGS_SET
                 = BindingFlags.Public
                 | BindingFlags.SetProperty
                 | BindingFlags.Instance
                 | BindingFlags.SetField
                 | BindingFlags.Static
                 ;
        public static IUnityContainer ApplicationContainer
        {
            get
            {
                return WebSingleton.ApplicationContainer;
            }
        }
        public static  void InitUnity(IUnityContainer container)
        {
            
                if (WebSingleton.ApplicationContainer == null)
                {

                    //IOC容器注册
                    try
                    {
                        object obj;
                        Type type = Type.GetType("Exhibition.Service.CenterService,Exhibition.Service");
                        Type formType;
                        Type toType;

                        PropertyInfo[] properties = type.GetProperties(BINDING_FLAGS_SET);
                        //IUnityContainer container = new UnityContainer();
                        //ManagerWebInit.InitUnity(container);
                        WebSingleton.ApplicationContainer = container;

                        ApplicationContainer.BuildUp(typeof(ManagerWebInit));


                        foreach (PropertyInfo propertie in properties)
                        {
                            string pname = propertie.Name;
                            string IServiceName = pname;
                            XElement ioc = GetServiceName(IServiceName);
                            if (ioc != null)
                            {
                                formType = Type.GetType(ioc.Attribute("IService").Value);
                                toType = Type.GetType(ioc.Attribute("Service").Value);
                                container.RegisterType(formType, toType);
                                obj = WebSingleton.ApplicationContainer.Resolve(formType);

                                propertie.SetValue(typeof(ManagerWebInit), obj, null);
                            }
                        }
                    }

                    catch (Exception ex)
                    {
                        LogHelper.Error("错误信息：" + ex.Message+ex.StackTrace);
                        throw (ex);
                    }

                }

             
            //container
                
            //    .RegisterType<IArchivesService, ArchivesService>()
            //    .RegisterType<IAreaService, AreaService>()
            //    .RegisterType<ICarPicService,CarPicService>()
            //    .RegisterType<ICarPriceService,CarPriceService>()
            //    .RegisterType<ICarService, CarService>()
            //    .RegisterType<ICarTypeService,CarTypeService>()
            //    .RegisterType<ICompUserLogService, CompUserLogService>()
            //    .RegisterType<ICompUserService, CompUserService>()
            //    .RegisterType<ICompUserShopService, CompUserShopService>()
            //    .RegisterType<IEnumService, EnumService>()
            //    .RegisterType<IGuideService, GuideService>()
            //    .RegisterType<IHotelGradeService, HotelGradeService>()
            //    .RegisterType<IHotelRoomService, HotelRoomService>()
            //    .RegisterType<IHotelService, HotelService>()
            //    .RegisterType<INewsClassService, NewsClassService>()
            //    .RegisterType<IOutletsService, OutletsService>()
            //    .RegisterType<IProductIcoService, ProductIcoService>()
            //    .RegisterType<IProductModelExtensionService, ProductModelExtensionService>()
            //    .RegisterType<IProductService, ProductService>()
            //    .RegisterType<IProductPriceService, ProductPriceService>()
            //    .RegisterType<IScenicImgService, ScenicImgService>()
            //    .RegisterType<IScenicService, ScenicService>()
            //    .RegisterType<ISubjectService, SubjectService>()
            //    .RegisterType<IUserCompService,UserCompService>()
            //    .RegisterType<ISafeService,SafeService>()
            //    .RegisterType<IUserService,UserService>()
            //    .RegisterType<ICardRuleService,CardRuleService>()
            //    .RegisterType<ICardService,CardService>()
            //    .RegisterType<IProductContentService,ProductContentService>()
            //    .RegisterType<ICardGroupService,CardGroupService>()
            //    .RegisterType<IExchangeBatchService,ExchangeBatchService>()
            //    .RegisterType<IExchangeProductService,ExchangeProductService>()
            //    .RegisterType<ICardOrderService,CardOrderService>()
            //    .RegisterType<IOrderService,OrderService>()
            //    .RegisterType<ISysConfigService,SysConfigService>()
            //    .RegisterType<ISysMenuService,SysMenuService>()
            //    .RegisterType<IUserSysRightService,UserSysRightService>()
            //    .RegisterType<IOrderUserService,OrderUserService>()
            //    .RegisterType<IMessageService,MessageService>()
            //    .RegisterType<IExchangeService,ExchangeService>()
            //    .RegisterType<IAdvService, AdvService>()
            //    .RegisterType<IAdvTypeService, AdvTypeService>() 
            //    .RegisterType<IArchivesCommentService,ArchivesCommentService>()
            //    .RegisterType<ICarProductService,CarProductService>()
            //    .RegisterType<IUserRightService, UserRightService>();

        }
        /// <summary>
        /// 根据XML获取IOC配置
        /// </summary>
        /// <param name="IServiceName"></param>
        /// <returns></returns>
        private static XElement GetServiceName(string IServiceName)
        {
            XElement config = XElement.Load(HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["IOCConfig"]));
            var tables = from ele in config.Elements("IOCRegister")
                         where ele.Attribute("id").Value == IServiceName
                         select ele;

            return tables.FirstOrDefault();
        }
    }
}
